﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Common;

public class Action_1 : ActionBase
{
    public override void Update()
    {
        base.Update();
        process();
    }
    public override void Start()
    {
        base.Start();//need do first

        AppInterface.ActionModule.RegisterEffect(new TrailEffect(Handle.Weapon.transform.Find("trail"), Animation, .3f, .5f));
        Excursion = new Vector2(.5f, 0);
        endTime = .5f;
        startTime = .3f;
        End = ready = false;
    }

    private Dictionary<int, IUnit> hitList = new Dictionary<int, IUnit>();
    private float endTime;
    private float startTime;
    private bool ready = false;
    private void process()
    {
        if (ready)
        {
            if (!End && Animation.time >= endTime)
            {
                End = true;
                return;
            }
        }
        else
        {
            if (Animation.time >= startTime) ready = true;
        }
        if (ready && !End)
        {
            if (Define.Mode == GameMode.PK)
            {
                foreach (IAvatarHandle av in AppInterface.AvatarModule.SceneIPlayers)
                {
                    if (!hitList.ContainsKey(av.Id) && Handle.Weapon.bounds.Intersects(av.Collider.bounds))
                    {
                        hitList[av.Id] = av;
                        av.OnBeat(this, UnityEngine.Random.Range(-50, -30), Handle as IUnit, true);
                    }
                }
            }
            else
            {
                foreach (IUnit av in AppInterface.MonsterModule.LiveMonster)
                {
                    if (!hitList.ContainsKey(av.Id) && !av.Dead && AssetHelp.IsFront(Handle, av) && av.Collider.bounds.Intersects(Handle.Weapon.bounds))
                    {
                        hitList[av.Id] = av;
                        av.OnBeat(this, UnityEngine.Random.Range(-50, -30), Handle as IUnit, true);
                    }
                }
            }
        }
    }
}

